package cn.wolfcode.luowowo.acticle.service.impl;

import cn.wolfcode.luowowo.acticle.domain.Travel;
import cn.wolfcode.luowowo.acticle.domain.TravelContent;
import cn.wolfcode.luowowo.acticle.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.acticle.mapper.TravelMapper;
import cn.wolfcode.luowowo.acticle.query.HomeQueryObject;
import cn.wolfcode.luowowo.acticle.query.TravelQueryObject;
import cn.wolfcode.luowowo.acticle.service.ITravelService;
import cn.wolfcode.luowowo.acticle.vo.MyHomeStatisVO;
import cn.wolfcode.luowowo.common.query.QueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * 游记服务实现类
 */
@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;

    //查询所有游记
    @Override
    public PageInfo query(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrder());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    //删除游记
    @Override
    public void delete(Long id) {
        travelMapper.deleteByPrimaryKey(id);
    }
    //保存/更新
    @Override
    public Long saveOrUpdate(Travel travel) {
        //isPublic
        if(travel.getIsPublic() == null){
            travel.setIsPublic(false);
        }
        //author.id  在控制器完成

        //summary 1.后台人员阅读后提取出  2.直接截取前200字
        String content = travel.getTravelContent().getContent();
        if(content != null && content.length() >= 200){
            travel.setSummary(content.substring(0, 200));
        }else{
            travel.setSummary(content);
        }

        //lastUpdateTime
        travel.setLastUpdateTime(new Date());

        if(travel.getId() != null){
            travelMapper.updateByPrimaryKey(travel);
            //travelContent.content
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }else{
            //createTime sql中完成
            travelMapper.insert(travel);
            //travelContent.content
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        }
        return travel.getId();
    }
    //查询所有游记
    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    //修改游记状态
    @Override
    public void changeState(Long id, Integer state) {
        travelMapper.updateState(id, state);
    }

    //查询游记文章
    @Override
    public TravelContent getTravelContent(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    //查询游记
    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    //根据目的地id获取热度前三的游记
    @Override
    public List<Travel> getTravelTop3ByDestId(Long destId) {
        return travelMapper.selectTravelTop3ByDestId(destId);
    }

    //获取最新发表的前六篇游记
    @Override
    public List<Travel> selectByNewTop6(Long uid) {
        List<Travel> travels = travelMapper.selectByNewTop6(uid);
        return travels;
    }

    @Override
    public MyHomeStatisVO count(Long id) {
        List<Travel> travels = travelMapper.selectCountNum(id);
        MyHomeStatisVO vo = new MyHomeStatisVO();
        for (Travel travel : travels) {
            vo.setCountNum(vo.getCountNum()+1);
            vo.setVieNum(vo.getVieNum()+travel.getViewnum());
            vo.setAllCount(vo.getAllCount()+travel.getViewnum()+travel.getReplynum()+travel.getFavornum()+travel.getSharenum()+travel.getThumbsupnum());
        }
        return vo;
    }

    @Override
    public PageInfo<Travel> queryForPage(HomeQueryObject qo, Long uid) {
        qo.setPageSize(11);
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getSort());
        List<Travel> travels = travelMapper.queryForPage(qo,uid);
        return new PageInfo<>(travels);

    }

}
